// pages/orderDetail/refund.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    productList:[],
    totalMoney:0, //退款金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options && options.id){
      this.setData({
        id:parseInt(options.id),
      })
      this.getOrderDetail()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getOrderDetail:function(e){
    var that=this
    var id=this.data.id
    var data={}
    data.id=id
   

    app.agriknow.getOrderDetail(data,'ht/store/userOrderDetails').then(res=>{
      var datas=res.data
      datas.proList.map(item=>{
        item.refundNum=0
      })
      that.setData({
        detail:datas,
        productList:datas.proList
      })
  
    })
  },

  //减少
  reduce:function(e){
    var productList=this.data.productList
    console.log(e)
    var aindex=e.currentTarget.dataset.aindex
    if(aindex>=0){
      this.setData({
        attrIndex:aindex
      })
    }
    var proIndex=e.currentTarget.dataset.index

    var refundNum=productList[proIndex].refundNum
    refundNum--
    if(refundNum<0){
      refundNum=0
    }
    var key='productList['+proIndex+'].refundNum'
    console.log(key)
    this.setData({
        [key]:refundNum
    })
    this.upRefund()
  },
  //增加
  add:function(e){

    var productList=this.data.productList

    var proIndex=e.currentTarget.dataset.index
    var refundNum=productList[proIndex].refundNum
    var num=productList[proIndex].num
    if(num>refundNum){
      refundNum++
    }else{
      refundNum=num
      return false
    }

    var key='productList['+proIndex+'].refundNum'
    console.log(key)
    this.setData({
        [key]:refundNum
    })

    this.upRefund()
  },

  upRefund:function(){
    var that=this
    var productList=this.data.productList
    var jsonArr=[]
    var totalMoney=0
    productList.map((item,index)=>{
      if(item.refundNum>0){
        var money = Number(item.price*item.refundNum).toFixed(2)
        totalMoney += Number(money)
        item.refundMoney=Number(money)
      }
    })
    this.setData({
      totalMoney:totalMoney
    })

  },
  refund:function(){
    var that=this
    var productList=this.data.productList
    var data={}
    data.orderId=this.data.id
    data.price=this.data.totalMoney
    var refundProList=productList.filter(item=>{return item.refundNum>0})
    if(refundProList.length==0){
      wx.showToast({
        title: '退款商品数量为0',
        icon:'none',
        duration:1500,
        mask:true
      })
      return false
    }
    data.refundProList=productList
    console.log(data)
    wx.showModal({
      title: '温馨提示',
      content: '是否确认给客户退款',
      success: function (res) {
        if (res.confirm) {
          app.agriknow.commRequest('ht/store/refundOrderAuditing',data).then(res=>{
            console.log(res)
            if(res.code != '000000') {
              wx.showToast({
                title: res.data.data,
                icon:'none',
                duration:1500,
                mask:true
              })
             
          }else{
            var pages = getCurrentPages(); 
            var prevPage = pages[pages.length - 2];   //上一页
          
             setTimeout(function(){
              wx.navigateBack({
                success: function () {
                  prevPage.onLoad(); // 执行前一个页面的onLoad方法
                }
              })
             },2000)
          }
          
           
          })
        } else if (res.cancel) {
          console.log('取消删除')
        }
      }
    })
  }
})